// Copyright [2020] <unknown>
#include <bits/stdc++.h>
using namespace std;
// #define ONLINE_JUDGE
#ifndef ONLINE_JUDGE
#define dbg(x...) { cerr << "\033[32;1m" << #x << " -> "; err(x); }
void err() { cerr << "\033[39;0m" << endl; }
template<typename T, typename... A>
void err(T a, A... x) { cerr << a << ' '; err(x...); }
#else
#define dbg(...)
#endif

#define sz(x) ((int)x.size())

typedef long long LL;

int t, n;

const int N = 550;


// struct node {
//     int v, x;
//     node (int v=0, int x=0) : v(v), x(x) {}
// };

int dp[N][N];

int d[N];

inline void update(int &a, int b) {
    if (a == -1)
        a = b;
    else
        a = min(a, b);
}

int main(int argc, char const *argv[]) {
    // code
    scanf("%d", &n);
    for (int i=1; i<=n; ++i) {
        scanf("%d", &dp[i][i]);
    }

    for (int i=2; i<=n; ++i) {
        for (int j=1; j<=n-i+1; ++j) {
            int l = j, r = j + i - 1;
            for (int k=l; k<=r; ++k) {
                if (dp[l][k] == dp[k+1][r] && dp[l][k] != 0) {
                    dp[l][r] = dp[l][k] + 1;
                    break;
                }
            }
        }
    }
    int now = 1;
    int ans = 0;
    while (now <= n) {
        for (int i=n; i>=now; --i) {
            if (dp[now][i]) {
                now = i + 1;
                ++ans;
                break;
            }
        }
    }

    memset(d, -1, sizeof d);
    d[0] = 0;
    for (int i=1; i<=n; ++i) {
        for (int j=0; j<i; ++j) {
            if (~d[j] && dp[j+1][i] > 0)
            {
                update(d[i], d[j] + 1);
            }
        }
    }
    cout << d[n] << endl;
    return 0;
}
